package com.work.controller;

import com.alibaba.fastjson.JSON;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.work.pojo.Address;
import com.work.pojo.User;
import com.work.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Properties;

/**
 * 用户控制器
 *
 * @author gy
 * &#064;date 2022/09/29
 * @version 1.0.0
 * @since 1.0.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/logOut")
    public ModelAndView logOut(ModelAndView modelAndView,HttpSession session){
        session.invalidate();
        modelAndView.setViewName( "redirect:/index.jsp" );
        return modelAndView;
    }

        @RequestMapping("/userLogin")
    public ModelAndView userLogin(HttpSession session, HttpServletResponse response, String password, String username, String vcode, ModelAndView mv, String auto) {
        System.out.println( username );
        System.out.println( password );
        if (auto != null) {
            try {
                String encode = URLEncoder.encode( username + "#" + password, "utf-8" );
                Cookie cookie = new Cookie( "user", encode );
                cookie.setDomain( "localhost" );
                cookie.setMaxAge( 14 * 24 * 60 * 60 );
                cookie.setPath( "/" );

                response.addCookie( cookie );
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        User user = userService.login( username, password );
        System.out.println( user );
        if (user != null) {
            session.setAttribute( "user", user );
            mv.setViewName( "redirect:/index.jsp" );
        } else {
            mv.setViewName( "redirect:/login.jsp" );
        }

        return mv;
    }

    @PostMapping("/register")
    public ModelAndView register(User user, ModelAndView mv) {
        System.out.println( user );
        boolean isSuccess = userService.register( user );
        System.out.println( isSuccess );
        if (isSuccess) {
            mv.setViewName( "redirect:/login.jsp" );
        }
        return mv;
    }

    @GetMapping("/checkUserName")
    public String checkUserName(String username) {
        boolean isSuccess = userService.checkUserName( username );
        if (isSuccess) {
            return "1";
        } else {
            return "2";
        }
    }

    @GetMapping("/deleteUser")
    public String deleteUser(Integer id) {
        if (id != null) {
            userService.deleteUserById( id );
        }
        List<User> list = userService.getAllUserList();
        System.out.println( "___________________________________________________" );
        for (User user : list) {
            System.out.println( user );
        }
        String jsonString = JSON.toJSONString( list );
        return jsonString;
    }

    @PostMapping("/searchUser")
    public String searchUser(String username, String gender) {
        List<User> users = userService.searchUserByNameAndGender( username, gender );
        String s = JSON.toJSONString( users );
        return s;
    }

    @GetMapping("/getUserList")
    public String getUserList() {
        List<User> list = userService.getAllUserList();
        String jsonString = JSON.toJSONString( list );
        return jsonString;
    }

    @GetMapping("/checkCode")
    public String checkCode(HttpSession session, String code) {
        String realCap = (String) session.getAttribute( "captcha" );
        if (realCap.equalsIgnoreCase( code )) {
            return "0";
        }
        return "1";
    }
    @RequestMapping("/getAddress")
    public ModelAndView getAddress(HttpSession session,ModelAndView modelAndView) {
        User user = (User) session.getAttribute( "user" );
        Integer id = user.getId();
        List  address= userService.getAddress(id);
        modelAndView.addObject( "addList",address );
        modelAndView.setViewName( "forward:/self_info.jsp" );
        return modelAndView;
    }
    @RequestMapping("/deleteAddress")
    public ModelAndView deleteAddress(Integer id,ModelAndView modelAndView) {
        userService.deleteAddress(id);
        modelAndView.setViewName( "redirect:/user/getAddress" );
        return modelAndView;
    }

    @RequestMapping("/defaultAddress")
    public ModelAndView defaultAddress(Integer id,ModelAndView modelAndView,HttpSession session) {
        User user = (User) session.getAttribute( "user" );
        Integer uid = user.getId();
        userService.defaultAddress(id,uid);
        modelAndView.setViewName( "redirect:/user/getAddress" );
        return modelAndView;
    }

    @RequestMapping("/updateAddress")
    public ModelAndView updateAddress(String phone,String id,String level,String name,String detail,ModelAndView modelAndView,HttpSession session) {
        User user = (User) session.getAttribute( "user" );
        Integer uid = user.getId();
        Address address = new Address();
        address.setName( name );
        address.setDetail( detail );
        address.setPhone( phone );
        address.setLevel( Integer.parseInt(  level) );
        address.setUid( uid );
        address.setId( Integer.parseInt(  id ));
        userService.updateAddress(address);
        modelAndView.setViewName( "redirect:/user/getAddress" );
        return modelAndView;
    }

    @RequestMapping("/addAddress")
    public ModelAndView addAddress(String phone, String id, String level, String name, String detail, ModelAndView modelAndView, HttpSession session) {
        User user = (User) session.getAttribute( "user" );
        Integer uid = user.getId();
        Address address = new Address();
        address.setName( name );
        address.setDetail( detail );
        address.setPhone( phone );
        address.setLevel(0);
        address.setUid( uid );
        userService.addAddress(address);
        modelAndView.setViewName( "redirect:/user/getAddress" );
        return modelAndView;
    }
}
